package com.lanchuan.soft.service.impl;

import com.lanchuan.soft.config.SystemVariable;
import com.lanchuan.soft.dao.*;
import com.lanchuan.soft.entity.*;
import com.lanchuan.soft.service.IEventService;
import com.lanchuan.soft.utils.CommonUtils;
import com.lanchuan.soft.utils.SystemConstant;
import lombok.extern.log4j.Log4j2;
import net.sf.json.JSONObject;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Log4j2
public class EventService implements IEventService {

    private IEventDao eventDao;
    private IEventStepDao eventStepDao;
    private IEventStepImageDao eventStepImageDao;
    private IUserDao userDao;
    private IDictionaryDao dictionaryDao;

    @Autowired
    public EventService(IEventDao eventDao, IEventStepDao eventStepDao, IEventStepImageDao eventStepImageDao, IUserDao userDao, IDictionaryDao dictionaryDao) {
        this.eventDao = eventDao;
        this.eventStepDao = eventStepDao;
        this.eventStepImageDao = eventStepImageDao;
        this.userDao = userDao;
        this.dictionaryDao = dictionaryDao;
    }

    @Override
    public Map<String, Object> findEventListByCondition(ParamEvent paramEvent) {

        Map<String, Object> resultMap = new HashMap<>();

        // 验证参数
        if (StringUtils.isBlank(paramEvent.getOperator())) {
            resultMap.put("errcode", SystemConstant.ILLEGAL_DATA);
            return resultMap;
        }

        try {
            // 当前用户
            ParamUser paramUser = new ParamUser();
            paramUser.setUserId(paramEvent.getOperator());
            paramUser.setState(SystemConstant.USER_STATE_NORMAL);
            User operator = userDao.findByCondition(paramUser);
            if (operator == null) {
                resultMap.put("errcode", SystemConstant.USER_NOT_EXISTING);
                return resultMap;
            }

            paramEvent.setOrganization(operator.getOrganization());
            List<Event> eventList = eventDao.findPagingListByCondition(paramEvent);
            int amount = eventDao.findCountByCondition(paramEvent);

            Map<String, Object> dataMap = new HashMap<>();
            dataMap.put("list", eventList);
            dataMap.put("amount", amount);

            resultMap.put("errcode", SystemConstant.SUCCESS);
            resultMap.put("data", dataMap);

            log.info(JSONObject.fromObject(resultMap).toString());
            return resultMap;
        } catch (Exception e) {
            log.error("EventService / findEventListByCondition", e);
            resultMap.put("errcode", SystemConstant.UNKNOWN_ERROR);
            return resultMap;
        }
    }

    @Transactional
    @Override
    public Map<String, Object> createEvent(ParamEvent paramEvent) {

        Map<String, Object> resultMap = new HashMap<>();

        // 验证参数
        if (StringUtils.isBlank(paramEvent.getAddress()) || StringUtils.isBlank(paramEvent.getDescription())
                || StringUtils.isBlank(paramEvent.getOperator())) {
            resultMap.put("errcode", SystemConstant.ILLEGAL_DATA);
            return resultMap;
        }

        try {
            // 当前用户
            ParamUser paramUser = new ParamUser();
            paramUser.setUserId(paramEvent.getOperator());
            paramUser.setType(SystemConstant.USER_TYPE_WORKER);
            paramUser.setState(SystemConstant.USER_STATE_NORMAL);
            User operator = userDao.findByCondition(paramUser);
            if (operator == null) {
                resultMap.put("errcode", SystemConstant.USER_NOT_EXISTING);
                return resultMap;
            }

            // 街道中端平台
            ParamUser pu = new ParamUser();
            pu.setOrganization(operator.getOrganization());
            pu.setType(SystemConstant.USER_TYPE_PLATFORM);
            pu.setState(SystemConstant.USER_STATE_NORMAL);
            User platform = userDao.findByCondition(pu);
            if (platform == null) {
                resultMap.put("errcode", SystemConstant.USER_NOT_EXISTING);
                return resultMap;
            }

            // 创建新的event
            Event event = new Event();
            event.setId(CommonUtils.createKeyId());
            event.setOrganization(operator.getOrganization());
            event.setAddress(paramEvent.getAddress());
            event.setAssigned(platform.getId());
            event.setStep(SystemConstant.EVENT_STEP_CREATE);
            int createEventResult = eventDao.create(event);
            if (createEventResult == 0) {
                resultMap.put("errcode", SystemConstant.UNKNOWN_ERROR);
                return resultMap;
            }

            paramEvent.setEventId(event.getId());
            paramEvent.setStep(SystemConstant.EVENT_STEP_CREATE);
            EventStep eventStep = createEventStep(paramEvent);
            int createEventStepResult = eventStepDao.create(eventStep);
            if (createEventStepResult == 0) {
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                resultMap.put("errcode", SystemConstant.UNKNOWN_ERROR);
                return resultMap;
            }

            resultMap.put("errcode", SystemConstant.SUCCESS);
            log.info(JSONObject.fromObject(resultMap).toString());
            return resultMap;
        } catch (Exception e) {
            log.error("EventService / createEvent", e);
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            resultMap.put("errcode", SystemConstant.UNKNOWN_ERROR);
            return resultMap;
        }
    }

    @Override
    public Map<String, Object> createResolveEvent(ParamEvent paramEvent) {

        Map<String, Object> resultMap = new HashMap<>();

        // 验证参数
        if (StringUtils.isBlank(paramEvent.getAddress()) || StringUtils.isBlank(paramEvent.getDescription())
                || StringUtils.isBlank(paramEvent.getOperator())) {
            resultMap.put("errcode", SystemConstant.ILLEGAL_DATA);
            return resultMap;
        }

        try {
            // 当前用户
            ParamUser paramUser = new ParamUser();
            paramUser.setUserId(paramEvent.getOperator());
            paramUser.setType(SystemConstant.USER_TYPE_WORKER);
            paramUser.setState(SystemConstant.USER_STATE_NORMAL);
            User operator = userDao.findByCondition(paramUser);
            if (operator == null) {
                resultMap.put("errcode", SystemConstant.USER_NOT_EXISTING);
                return resultMap;
            }

            // 创建新的event
            Event event = new Event();
            event.setId(CommonUtils.createKeyId());
            event.setOrganization(operator.getOrganization());
            event.setAddress(paramEvent.getAddress());
            event.setAssigned(operator.getId());
            event.setStep(SystemConstant.EVENT_STEP_SOLVE_BY_WORKER);
            int createEventResult = eventDao.create(event);
            if (createEventResult == 0) {
                resultMap.put("errcode", SystemConstant.UNKNOWN_ERROR);
                return resultMap;
            }

            paramEvent.setEventId(event.getId());
            paramEvent.setStep(SystemConstant.EVENT_STEP_CREATE);
            EventStep startEventStep = createEventStep(paramEvent);
            int createStartEventStepResult = eventStepDao.create(startEventStep);
            if (createStartEventStepResult == 0) {
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                resultMap.put("errcode", SystemConstant.UNKNOWN_ERROR);
                return resultMap;
            }

            EventStep endEventStep = new EventStep();
            endEventStep.setId(CommonUtils.createKeyId());
            endEventStep.setEventId(event.getId());
            endEventStep.setDescription(paramEvent.getResolvedDescription());
            endEventStep.setImages(paramEvent.getResolvedImages());
            endEventStep.setOperator(paramEvent.getOperator());
            endEventStep.setStep(SystemConstant.EVENT_STEP_SOLVE_BY_WORKER);
            int createEndEventStepResult = eventStepDao.create(endEventStep);
            if (createEndEventStepResult == 0) {
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                resultMap.put("errcode", SystemConstant.UNKNOWN_ERROR);
                return resultMap;
            }

            resultMap.put("errcode", SystemConstant.SUCCESS);
            log.info(JSONObject.fromObject(resultMap).toString());
            return resultMap;
        } catch (Exception e) {
            log.error("EventService / createResolveEvent", e);
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            resultMap.put("errcode", SystemConstant.UNKNOWN_ERROR);
            return resultMap;
        }
    }

    @Transactional
    @Override
    public Map<String, Object> updateEventType(ParamEvent paramEvent) {

        Map<String, Object> resultMap = new HashMap<>();

        // 验证参数
        if (StringUtils.isBlank(paramEvent.getEventId()) || StringUtils.isBlank(paramEvent.getOperator())
                || StringUtils.isBlank(paramEvent.getType())) {
            resultMap.put("errcode", SystemConstant.ILLEGAL_DATA);
            return resultMap;
        }

        try {
            // 当前用户
            ParamUser paramUser = new ParamUser();
            paramUser.setUserId(paramEvent.getOperator());
            paramUser.setType(SystemConstant.USER_TYPE_PLATFORM);
            paramUser.setState(SystemConstant.USER_STATE_NORMAL);
            User operator = userDao.findByCondition(paramUser);
            if (operator == null) {
                resultMap.put("errcode", SystemConstant.USER_NOT_EXISTING);
                return resultMap;
            }

            // 事件
            ParamEvent pe = new ParamEvent();
            pe.setEventId(paramEvent.getEventId());
            pe.setOrganization(operator.getOrganization());
            pe.setStep(SystemConstant.EVENT_STEP_CREATE);
            Event event = eventDao.findByCondition(pe);
            if (event == null) {
                resultMap.put("errcode", SystemConstant.EVENT_NOT_EXISTING);
                return resultMap;
            }

            // 事件分类
            String[] type = paramEvent.getType().split("-");
            ParamDictionary paramDictionary = new ParamDictionary();
            paramDictionary.setDictionaryId(type[type.length - 1]);
            paramDictionary.setType(SystemConstant.DICTIONARY_TYPE_EVENT_TYPE);
            Dictionary dictionary = dictionaryDao.findByCondition(paramDictionary);
            if (dictionary == null) {
                resultMap.put("errcode", SystemConstant.EVENT_TYPE_NOT_EXISTING);
                return resultMap;
            }

            // 添加事件进度
            paramEvent.setStep(SystemConstant.EVENT_STEP_UPDATE_CLASSIFY);
            EventStep eventStep = createEventStep(paramEvent);
            int createEventStepResult = eventStepDao.create(eventStep);
            if (createEventStepResult == 0) {
                resultMap.put("errcode", SystemConstant.UNKNOWN_ERROR);
                return resultMap;
            }

            // 更新事件进度
            paramEvent.setAssigned(operator.getId());
            paramEvent.setType(paramEvent.getType());
            int updateResult = eventDao.update(paramEvent);
            if (updateResult == 0) {
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                resultMap.put("errcode", SystemConstant.UNKNOWN_ERROR);
                return resultMap;
            }

            resultMap.put("errcode", SystemConstant.SUCCESS);
            log.info(JSONObject.fromObject(resultMap).toString());
            return resultMap;
        } catch (Exception e) {
            log.error("EventService / updateEventType", e);
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            resultMap.put("errcode", SystemConstant.UNKNOWN_ERROR);
            return resultMap;
        }
    }

    @Transactional
    @Override
    public Map<String, Object> reportToLeader(ParamEvent paramEvent) {

        Map<String, Object> resultMap = new HashMap<>();

        // 验证参数
        if (StringUtils.isBlank(paramEvent.getEventId()) || StringUtils.isBlank(paramEvent.getOperator())) {
            resultMap.put("errcode", SystemConstant.ILLEGAL_DATA);
            return resultMap;
        }

        try {
            // 当前用户
            ParamUser paramUser = new ParamUser();
            paramUser.setUserId(paramEvent.getOperator());
            paramUser.setType(SystemConstant.USER_TYPE_PLATFORM);
            paramUser.setState(SystemConstant.USER_STATE_NORMAL);
            User operator = userDao.findByCondition(paramUser);
            if (operator == null) {
                resultMap.put("errcode", SystemConstant.USER_NOT_EXISTING);
                return resultMap;
            }

            // 事件
            ParamEvent pe = new ParamEvent();
            pe.setEventId(paramEvent.getEventId());
            pe.setOrganization(operator.getOrganization());
            pe.setStep(SystemConstant.EVENT_STEP_UPDATE_CLASSIFY);
            Event event = eventDao.findByCondition(pe);
            if (event == null) {
                resultMap.put("errcode", SystemConstant.EVENT_NOT_EXISTING);
                return resultMap;
            }

            // 决策中心
            ParamUser pu = new ParamUser();
            pu.setOrganization(operator.getOrganization());
            pu.setType(SystemConstant.USER_TYPE_LEADER);
            pu.setState(SystemConstant.USER_STATE_NORMAL);
            User leader = userDao.findByCondition(pu);
            if (leader == null) {
                resultMap.put("errcode", SystemConstant.USER_NOT_EXISTING);
                return resultMap;
            }

            // 添加事件进度
            paramEvent.setStep(SystemConstant.EVENT_STEP_REPORT_TO_LEADER);
            EventStep eventStep = createEventStep(paramEvent);
            int createEventStepResult = eventStepDao.create(eventStep);
            if (createEventStepResult == 0) {
                resultMap.put("errcode", SystemConstant.UNKNOWN_ERROR);
                return resultMap;
            }

            // 更新事件进度
            paramEvent.setAssigned(leader.getId());
            paramEvent.setType(event.getType());
            int updateResult = eventDao.update(paramEvent);
            if (updateResult == 0) {
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                resultMap.put("errcode", SystemConstant.UNKNOWN_ERROR);
                return resultMap;
            }

            resultMap.put("errcode", SystemConstant.SUCCESS);
            log.info(JSONObject.fromObject(resultMap).toString());
            return resultMap;
        } catch (Exception e) {
            log.error("EventService / reportToLeader", e);
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            resultMap.put("errcode", SystemConstant.UNKNOWN_ERROR);
            return resultMap;
        }
    }

    @Transactional
    @Override
    public Map<String, Object> commandToPlatform(ParamEvent paramEvent) {

        Map<String, Object> resultMap = new HashMap<>();

        // 验证参数
        if (StringUtils.isBlank(paramEvent.getEventId()) || StringUtils.isBlank(paramEvent.getOperator())) {
            resultMap.put("errcode", SystemConstant.ILLEGAL_DATA);
            return resultMap;
        }

        try {
            // 当前用户
            ParamUser paramUser = new ParamUser();
            paramUser.setUserId(paramEvent.getOperator());
            paramUser.setType(SystemConstant.USER_TYPE_LEADER);
            paramUser.setState(SystemConstant.USER_STATE_NORMAL);
            User operator = userDao.findByCondition(paramUser);
            if (operator == null) {
                resultMap.put("errcode", SystemConstant.USER_NOT_EXISTING);
                return resultMap;
            }

            // 事件
            ParamEvent pe = new ParamEvent();
            pe.setEventId(paramEvent.getEventId());
            pe.setOrganization(operator.getOrganization());
            pe.setStep(SystemConstant.EVENT_STEP_REPORT_TO_LEADER);
            Event event = eventDao.findByCondition(pe);
            if (event == null) {
                resultMap.put("errcode", SystemConstant.EVENT_NOT_EXISTING);
                return resultMap;
            }

            // 街道中端平台
            ParamUser pu = new ParamUser();
            pu.setOrganization(operator.getOrganization());
            pu.setType(SystemConstant.USER_TYPE_PLATFORM);
            pu.setState(SystemConstant.USER_STATE_NORMAL);
            User platform = userDao.findByCondition(pu);
            if (platform == null) {
                resultMap.put("errcode", SystemConstant.USER_NOT_EXISTING);
                return resultMap;
            }

            // 添加事件进度
            paramEvent.setStep(SystemConstant.EVENT_STEP_COMMAND_TO_PLATFORM);
            EventStep eventStep = createEventStep(paramEvent);
            int createEventStepResult = eventStepDao.create(eventStep);
            if (createEventStepResult == 0) {
                resultMap.put("errcode", SystemConstant.UNKNOWN_ERROR);
                return resultMap;
            }

            // 更新事件进度
            paramEvent.setAssigned(platform.getId());
            paramEvent.setType(event.getType());
            int updateResult = eventDao.update(paramEvent);
            if (updateResult == 0) {
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                resultMap.put("errcode", SystemConstant.UNKNOWN_ERROR);
                return resultMap;
            }

            resultMap.put("errcode", SystemConstant.SUCCESS);
            log.info(JSONObject.fromObject(resultMap).toString());
            return resultMap;
        } catch (Exception e) {
            log.error("EventService / commandToPlatform", e);
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            resultMap.put("errcode", SystemConstant.UNKNOWN_ERROR);
            return resultMap;
        }
    }

    @Transactional
    @Override
    public Map<String, Object> commandToWorker(ParamEvent paramEvent) {

        Map<String, Object> resultMap = new HashMap<>();

        // 验证参数
        if (StringUtils.isBlank(paramEvent.getEventId()) || StringUtils.isBlank(paramEvent.getOperator())) {
            resultMap.put("errcode", SystemConstant.ILLEGAL_DATA);
            return resultMap;
        }

        try {
            // 当前用户
            ParamUser paramUser = new ParamUser();
            paramUser.setUserId(paramEvent.getOperator());
            paramUser.setType(SystemConstant.USER_TYPE_PLATFORM);
            paramUser.setState(SystemConstant.USER_STATE_NORMAL);
            User operator = userDao.findByCondition(paramUser);
            if (operator == null) {
                resultMap.put("errcode", SystemConstant.USER_NOT_EXISTING);
                return resultMap;
            }

            // 事件
            ParamEvent pe = new ParamEvent();
            pe.setEventId(paramEvent.getEventId());
            pe.setOrganization(operator.getOrganization());
            pe.setStep(SystemConstant.EVENT_STEP_CREATE);
            Event event = eventDao.findByCondition(pe);
            if (event == null) {
                resultMap.put("errcode", SystemConstant.EVENT_NOT_EXISTING);
                return resultMap;
            }

            // 验证工作人员
            ParamUser pu = new ParamUser();
            pu.setUserId(paramEvent.getAssigned());
            pu.setOrganization(operator.getOrganization());
            pu.setType(SystemConstant.USER_TYPE_WORKER);
            pu.setState(SystemConstant.USER_STATE_NORMAL);
            User worker = userDao.findByCondition(pu);
            if (worker == null) {
                resultMap.put("errcode", SystemConstant.USER_NOT_EXISTING);
                return resultMap;
            }

            // 添加事件进度
            paramEvent.setStep(SystemConstant.EVENT_STEP_COMMAND_TO_WORKER);
            EventStep eventStep = createEventStep(paramEvent);
            int createEventStepResult = eventStepDao.create(eventStep);
            if (createEventStepResult == 0) {
                resultMap.put("errcode", SystemConstant.UNKNOWN_ERROR);
                return resultMap;
            }

            // 更新事件进度
            paramEvent.setAssigned(worker.getId());
            paramEvent.setType(event.getType());
            int updateResult = eventDao.update(paramEvent);
            if (updateResult == 0) {
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                resultMap.put("errcode", SystemConstant.UNKNOWN_ERROR);
                return resultMap;
            }

            resultMap.put("errcode", SystemConstant.SUCCESS);
            log.info(JSONObject.fromObject(resultMap).toString());
            return resultMap;
        } catch (Exception e) {
            log.error("EventService / commandToWorker", e);
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            resultMap.put("errcode", SystemConstant.UNKNOWN_ERROR);
            return resultMap;
        }
    }

    @Transactional
    @Override
    public Map<String, Object> reportToDepartment(ParamEvent paramEvent) {

        Map<String, Object> resultMap = new HashMap<>();

        // 验证参数
        if (StringUtils.isBlank(paramEvent.getEventId()) || StringUtils.isBlank(paramEvent.getOperator())) {
            resultMap.put("errcode", SystemConstant.ILLEGAL_DATA);
            return resultMap;
        }

        try {
            // 当前用户
            ParamUser paramUser = new ParamUser();
            paramUser.setUserId(paramEvent.getOperator());
            paramUser.setType(SystemConstant.USER_TYPE_PLATFORM);
            paramUser.setState(SystemConstant.USER_STATE_NORMAL);
            User operator = userDao.findByCondition(paramUser);
            if (operator == null) {
                resultMap.put("errcode", SystemConstant.USER_NOT_EXISTING);
                return resultMap;
            }

            // 事件
            ParamEvent pe = new ParamEvent();
            pe.setEventId(paramEvent.getEventId());
            pe.setOrganization(operator.getOrganization());
            pe.setStep(SystemConstant.EVENT_STEP_COMMAND_TO_PLATFORM);
            Event event = eventDao.findByCondition(pe);
            if (event == null) {
                resultMap.put("errcode", SystemConstant.EVENT_NOT_EXISTING);
                return resultMap;
            }

            // 验证有关部门
            ParamUser pu = new ParamUser();
            pu.setUserId(paramEvent.getAssigned());
            pu.setType(SystemConstant.USER_TYPE_DEPARTMENT);
            pu.setState(SystemConstant.USER_STATE_NORMAL);
            User department = userDao.findByCondition(pu);
            if (department == null) {
                resultMap.put("errcode", SystemConstant.USER_NOT_EXISTING);
                return resultMap;
            }

            // 添加事件进度
            paramEvent.setStep(SystemConstant.EVENT_STEP_REPORT_TO_DEPARTMENT);
            EventStep eventStep = createEventStep(paramEvent);
            int createEventStepResult = eventStepDao.create(eventStep);
            if (createEventStepResult == 0) {
                resultMap.put("errcode", SystemConstant.UNKNOWN_ERROR);
                return resultMap;
            }

            // 更新事件进度
            paramEvent.setAssigned(department.getId());
            paramEvent.setType(event.getType());
            int updateResult = eventDao.update(paramEvent);
            if (updateResult == 0) {
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                resultMap.put("errcode", SystemConstant.UNKNOWN_ERROR);
                return resultMap;
            }

            resultMap.put("errcode", SystemConstant.SUCCESS);
            log.info(JSONObject.fromObject(resultMap).toString());
            return resultMap;
        } catch (Exception e) {
            log.error("EventService / reportToDepartment", e);
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            resultMap.put("errcode", SystemConstant.UNKNOWN_ERROR);
            return resultMap;
        }
    }

    @Transactional
    @Override
    public Map<String, Object> solveByWorker(ParamEvent paramEvent) {

        Map<String, Object> resultMap = new HashMap<>();

        // 验证参数
        if (StringUtils.isBlank(paramEvent.getEventId()) || StringUtils.isBlank(paramEvent.getOperator())) {
            resultMap.put("errcode", SystemConstant.ILLEGAL_DATA);
            return resultMap;
        }

        try {
            // 当前用户
            ParamUser paramUser = new ParamUser();
            paramUser.setUserId(paramEvent.getOperator());
            paramUser.setType(SystemConstant.USER_TYPE_WORKER);
            paramUser.setState(SystemConstant.USER_STATE_NORMAL);
            User operator = userDao.findByCondition(paramUser);
            if (operator == null) {
                resultMap.put("errcode", SystemConstant.USER_NOT_EXISTING);
                return resultMap;
            }

            // 事件
            ParamEvent pe = new ParamEvent();
            pe.setEventId(paramEvent.getEventId());
            pe.setOrganization(operator.getOrganization());
            pe.setStep(SystemConstant.EVENT_STEP_COMMAND_TO_WORKER);
            Event event = eventDao.findByCondition(pe);
            if (event == null) {
                resultMap.put("errcode", SystemConstant.EVENT_NOT_EXISTING);
                return resultMap;
            }

            // 添加事件进度
            paramEvent.setStep(SystemConstant.EVENT_STEP_SOLVE_BY_WORKER);
            EventStep eventStep = createEventStep(paramEvent);
            int createEventStepResult = eventStepDao.create(eventStep);
            if (createEventStepResult == 0) {
                resultMap.put("errcode", SystemConstant.UNKNOWN_ERROR);
                return resultMap;
            }

            // 更新事件进度
            paramEvent.setAssigned(operator.getId());
            paramEvent.setType(event.getType());
            int updateResult = eventDao.update(paramEvent);
            if (updateResult == 0) {
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                resultMap.put("errcode", SystemConstant.UNKNOWN_ERROR);
                return resultMap;
            }

            resultMap.put("errcode", SystemConstant.SUCCESS);
            log.info(JSONObject.fromObject(resultMap).toString());
            return resultMap;
        } catch (Exception e) {
            log.error("EventService / solveByWorker", e);
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            resultMap.put("errcode", SystemConstant.UNKNOWN_ERROR);
            return resultMap;
        }
    }

    @Transactional
    @Override
    public Map<String, Object> solveByDepartment(ParamEvent paramEvent) {

        Map<String, Object> resultMap = new HashMap<>();

        // 验证参数
        if (StringUtils.isBlank(paramEvent.getEventId()) || StringUtils.isBlank(paramEvent.getOperator())) {
            resultMap.put("errcode", SystemConstant.ILLEGAL_DATA);
            return resultMap;
        }

        try {
            // 当前用户
            ParamUser paramUser = new ParamUser();
            paramUser.setUserId(paramEvent.getOperator());
            paramUser.setType(SystemConstant.USER_TYPE_DEPARTMENT);
            paramUser.setState(SystemConstant.USER_STATE_NORMAL);
            User operator = userDao.findByCondition(paramUser);
            if (operator == null) {
                resultMap.put("errcode", SystemConstant.USER_NOT_EXISTING);
                return resultMap;
            }

            // 事件
            ParamEvent pe = new ParamEvent();
            pe.setEventId(paramEvent.getEventId());
            pe.setOrganization(operator.getOrganization());
            pe.setStep(SystemConstant.EVENT_STEP_REPORT_TO_DEPARTMENT);
            Event event = eventDao.findByCondition(pe);
            if (event == null) {
                resultMap.put("errcode", SystemConstant.EVENT_NOT_EXISTING);
                return resultMap;
            }

            // 添加事件进度
            paramEvent.setStep(SystemConstant.EVENT_STEP_SOLVE_BY_DEPARTMENT);
            EventStep eventStep = createEventStep(paramEvent);
            int createEventStepResult = eventStepDao.create(eventStep);
            if (createEventStepResult == 0) {
                resultMap.put("errcode", SystemConstant.UNKNOWN_ERROR);
                return resultMap;
            }

            // 更新事件进度
            paramEvent.setAssigned(operator.getId());
            paramEvent.setType(event.getType());
            int updateResult = eventDao.update(paramEvent);
            if (updateResult == 0) {
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                resultMap.put("errcode", SystemConstant.UNKNOWN_ERROR);
                return resultMap;
            }

            resultMap.put("errcode", SystemConstant.SUCCESS);
            log.info(JSONObject.fromObject(resultMap).toString());
            return resultMap;
        } catch (Exception e) {
            log.error("EventService / solveByDepartment", e);
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            resultMap.put("errcode", SystemConstant.UNKNOWN_ERROR);
            return resultMap;
        }
    }

    @Override
    public Map<String, Object> deleteEvent(ParamEvent paramEvent) {

        Map<String, Object> resultMap = new HashMap<>();

        // 验证参数
        if (StringUtils.isBlank(paramEvent.getEventId()) || StringUtils.isBlank(paramEvent.getOperator())) {
            resultMap.put("errcode", SystemConstant.ILLEGAL_DATA);
            return resultMap;
        }

        try {
            // 当前用户
            ParamUser paramUser = new ParamUser();
            paramUser.setUserId(paramEvent.getOperator());
            paramUser.setType(SystemConstant.USER_TYPE_PLATFORM);
            paramUser.setState(SystemConstant.USER_STATE_NORMAL);
            User operator = userDao.findByCondition(paramUser);
            if (operator == null) {
                resultMap.put("errcode", SystemConstant.USER_NOT_EXISTING);
                return resultMap;
            }

            // 删除事件
            int deleteResult = eventDao.delete(paramEvent);
            if (deleteResult == 0) {
                resultMap.put("errcode", SystemConstant.UNKNOWN_ERROR);
                return resultMap;
            }

            resultMap.put("errcode", SystemConstant.SUCCESS);
            log.info(JSONObject.fromObject(resultMap).toString());
            return resultMap;
        } catch (Exception e) {
            log.error("EventService / deleteEvent", e);
            resultMap.put("errcode", SystemConstant.UNKNOWN_ERROR);
            return resultMap;
        }
    }

    private EventStep createEventStep(ParamEvent paramEvent) {
        EventStep eventStep = new EventStep();
        eventStep.setId(CommonUtils.createKeyId());
        eventStep.setEventId(paramEvent.getEventId());
        eventStep.setDescription(paramEvent.getDescription());
        eventStep.setImages(paramEvent.getImages());
        eventStep.setOperator(paramEvent.getOperator());
        eventStep.setStep(paramEvent.getStep());
        return eventStep;
    }

    @Override
    public Map<String, Object> uploadImage(MultipartFile file) {

        Map<String, Object> resultMap = new HashMap<>();

        if (file.isEmpty()) {
            resultMap.put("errcode", SystemConstant.ILLEGAL_DATA);
            return resultMap;
        }

        try {
            String folder = CommonUtils.createUploadPath();
            File dest = new File(SystemVariable.getUploadSave() + folder);
            if (!dest.exists()) {
                boolean mkdirs = dest.mkdirs();
                if (!mkdirs) log.info("uploadImage");
            }

            String originalFileName = file.getOriginalFilename();
            String suffix = "";
            if (StringUtils.isNotBlank(originalFileName) && originalFileName.lastIndexOf(".") != -1) {
                suffix = originalFileName.substring(originalFileName.lastIndexOf("."));
            }
            String newFileName = CommonUtils.createKeyId() + suffix;
            file.transferTo(new File(dest + "/" + newFileName)); //保存文件

            EventStepImage eventStepImage = new EventStepImage();
            eventStepImage.setId(CommonUtils.createKeyId());
            eventStepImage.setUrl(SystemVariable.getUploadVisit() + "/" + folder + "/" + newFileName);
            int createResult = eventStepImageDao.create(eventStepImage);
            if (createResult == 0) {
                resultMap.put("errcode", SystemConstant.UNKNOWN_ERROR);
                return resultMap;
            }

            Map<String, Object> dataMap = new HashMap<>();
            dataMap.put("id", eventStepImage.getId());
            dataMap.put("url", eventStepImage.getUrl());

            resultMap.put("errcode", SystemConstant.SUCCESS);
            resultMap.put("data", dataMap);
            log.info(JSONObject.fromObject(resultMap).toString());
            return resultMap;
        } catch (IllegalStateException | IOException e) {
            log.error("EventService / uploadImage", e);
            resultMap.put("errcode", SystemConstant.UNKNOWN_ERROR);
            return resultMap;
        }
    }
}
